<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #box {
      position: relative;
      width: 100px;
      height: 100px;
      background-color: red;
    }
  </style>
</head>
<body>
  <input type="button" id="btn" value="动画 400">

  <div id="box">
    
  </div>
  <script>
    var btn = document.getElementById('btn');
    var box = document.getElementById('box');
    btn.onclick = function () {
      var timerId = setInterval(function () {
        // 步进  每次移动的距离
        var step = 10;
        var target = 400;

        // 盒子当前的位置
        var current = box.offsetLeft;

        if (current >= target) {
          // 让定时器停止
          clearInterval(timerId);
          // 让盒子到target的位置
          box.style.left = target + 'px';
          return;
        }
        // 移动盒子
        current += step;
        box.style.left = current + 'px';
      }, 30);

      // 30毫秒  视觉残留
    }
  </script>
</body>
</html>
